<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/12/8
 * Time: 17:20
 */

namespace app\admin\controller;
use app\Model\CommoditySpecModel;
use app\Model\CommodityTypeModel;
use think\Db;
use app\admin\Logic\SpecificationLogic;
use think\Request;
use app\Model\CommodityItemModel;
class Specification extends Common
{
    /**
     * 商品规格首页
     * $list 商品规格首页列表查询
     * foreach 规格项循环
     */
 public function specification(){
     $select=Db('commodity_type')->select();
     $list=Db('commodity_spec')
         ->alias('c')
         ->join('commodity_type t','c.type_id=t.id')
         ->join('commodity_item i','c.s_id=i.spec_id')
         ->group("s_id")
         ->order('c.type_id desc')
         ->paginate(10,false,[
             'query' => request()->param(), ]);
     $listAll = $list->all();//分页对象转化为数组
     $Specification=new SpecificationLogic();
     foreach($listAll as $k => $v)
     {       // 获取规格项
         $arr = $Specification->getSpecItem($v['s_id']);
         $listAll[$k]['item'] = implode(' , ', $arr);
     }
     $this->getConAndAct('Goods');
     $this->assign('select',$select);
     $this->assign('list',$list);
     $this->assign('listAll',$listAll);
     return $this->fetch('SpecificationIndex');
 }
 /*
  * 商品规格新增
  * */
 public function SpecificationAdd(){
     $type_id=input('type_id');
      $spec_name=input('spec_name');
      $order=input('order');
      $item=input('item');
     if(Request::instance()->isGet()) {
         $select = Db('commodity_type')->select();//查询类型
         $this->assign('select', $select);
         return $this->fetch('SpecificationAdd');
         exit();
     }
     //规格项为空时，新增到规格表
     if(empty($item)){
         $data=['spec_name'=>$spec_name,
             'order'=>$order,
             'type_id'=>$type_id
         ];
         $res=Db('commodity_spec')->insert($data);
         if ($res){
             $return_arr = array(
                 'status' => 1,
                 'msg'   => '操作成功',
             );
             echo json_encode($return_arr);
         }else{
             $return_arr = array(
                 'status' => -1,
                 'msg'   => '操作失败',
             );
             echo json_encode($return_arr);
         }
         die();
     }
     //不为空时2张表一起新增
     else{
         $item=explode("\r\n",$item);
         $data=['spec_name'=>$spec_name,
             'order'=>$order,
             'type_id'=>$type_id
         ];
         $res=Db('commodity_spec')->insertGetid($data);//返回自增id
        //循环数组
         foreach ($item as $k=>$v){
             $data=['spec_id'=>$res,
                 'item'=>$item[$k]
             ];
             $reg=Db('commodity_item')->insert($data);
         }
         if ($reg){
             $return_arr = array(
                 'status' => 1,
                 'msg'   => '操作成功',
             );
             echo json_encode($return_arr);
         }else{
             $return_arr = array(
                 'status' => -1,
                 'msg'   => '操作失败',
             );
             echo json_encode($return_arr);
         }
         die();
     }
 }
//商品规格更新
 public function SpecificationUpdate(){
     $type_id=input('type_id');
     $spec_name=input('spec_name');
     $order=input('order');
     $item=input('item');
     $id = input('id');
     if(Request::instance()->isGet()) {
         $select = Db('commodity_type')->select();//查询商品类型
         $list=Db('commodity_spec')//连表查询商品类型与规格
         ->alias('c')
             ->where('s_id',$id)
             ->join('commodity_type t','c.type_id=t.id')
             ->find();
         $Specification=new SpecificationLogic();
         $cat_list=Db('commodity_spec')//查询规格项并去重
             ->alias('c')
             ->where('s_id',$id)
             ->join('commodity_item i','c.s_id=i.spec_id')
             ->group("s_id")
             ->select();
         //循环输出规格项
         foreach ($cat_list as $k=>$v){
             $arr = $Specification->getSpecItem($v['s_id']);
             $cat_list[$k]['item']=implode(PHP_EOL,$arr);
         }
         $cat_list=$cat_list[$k]['item'];//将规格项的值副给$cat_list
         $this->assign('select', $select);
         $this->assign('list', $list);
         $this->assign('cat_list', $cat_list);
         return $this->fetch('SpecificationUpdate');
         exit();
     }
     $item=explode("\r\n",$item);//分割成数组
     $data=['spec_name'=>$spec_name,
         'order'=>$order,
         'type_id'=>$type_id
     ];
     $res=Db('commodity_spec')->where('s_id',$id)->update($data);//更新规格表
     //判断修改
         $c=Db('commodity_item')->where('spec_id',$id)->column('id');//返回id
         //删除原有数据
         $info=CommodityItemModel::destroy($c);

             //新增修改数据
             $datas =[];
             foreach ($item as $k=>$v){
                 $data=[
                     'spec_id'=>$id,
                     'item'=>$v
                 ];
                 array_push($datas,$data);
             }
             $ress=Db('commodity_item')->insertAll($datas);

     if ($res||$info){
         $return_arr = array(
             'status' => 1,
             'msg'   => '操作成功',
         );
         echo json_encode($return_arr);
     }else{
         $return_arr = array(
             'status' => -1,
             'msg'   => '操作失败',
         );
         echo json_encode($return_arr);
     }
 }
/*
 * 规格筛选查询
 * */
public function SpecificationSearch(){
    $id=input('type_id');
    $where=[];
    if ($id){
        $where['type_id']=$id;
    }

    $select=CommodityTypeModel::select();

    $list=CommoditySpecModel::
        alias('c')
        ->where($where)
        ->join('commodity_type t','c.type_id=t.id')
        ->order('order', 'desc')
        ->paginate(null,false,['query' => request()->param()]);
    $listAll = $list->all();//分页对象转化为数组
    $Specification=new SpecificationLogic();
    foreach($listAll as $k => $v)
    {       // 获取规格项
        $arr = $Specification->getSpecItem($v['s_id']);
        $listAll[$k]['item'] = implode(' , ', $arr);
    }
    $this->assign('select',$select);
    $this->assign('list',$list);
    $this->assign('listAll',$listAll);
    return $this->fetch('SpecificationIndex');
}
    /**
     * 删除商品规格
     */
    public function del()
    {
        $s_id=input('id');
        // 判断 商品规格项

        CommodityItemModel::where("spec_id",$s_id)->delete();

        // 删除分类

       $res= CommoditySpecModel::where("s_id",$s_id)->delete();
       if (!$res){
           return retJson('',200,'删除失败');
       }
       return retJson('',200,'删除成功');
    }

}